package com.laity.add;

import java.util.concurrent.CyclicBarrier;

/**
 * @author: Laity
 * @Project: JavaLaity
 * @Package: com.laity.add.CyclicBarrierDemo
 * @Date: 2023年11月26日 22:58
 * @Description: CyclicBarrier 加法计数器
 */

public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("召唤神龙");
        });
        /*
        集齐七颗龙珠，召唤神龙
         */
        for (int i = 1; i <= 7; i++) {
            final int index = i;
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " 收集到" + index + "颗龙珠");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "Thread-" + i).start();
        }
    }

}
